"""
@FileName：company_core_data_monthly.py\n
@Description：\n
@Author：代玉铃\n
@Time：2024/4/8 11：51\n
@Copyright：©2019-2024 研发中心
"""
import scrapy
from lxml import etree

from ...hs_compay_hexin.items import company_core_data_monthly

class Demo1Spider(scrapy.Spider):
    name = "company_core"
    allowed_domains = ["quote.eastmoney.com"]
    start_urls = ["https://quote.eastmoney.com/center/gridlist.html#hs_a_board"]

    def parse(self, response):
        html = etree.HTML(response.text)
        a_list = html.xpath('//*[@class="mywidth"]/a/@href')
        for a in a_list:
            detail_url = response.urljoin(a)
            print(a)
            yield scrapy.Request(
                url=detail_url,
                callback=self.parse_detail,
                dont_filter=False
            )

    def parse_detail(self, response):
        def value_empty(value_):
            if (value_):
                return value_[0].replace("亿","").replace("万","").replace("%","")
            else:
                return 0
        item=company_core_data_monthly()
        html_detail = etree.HTML(response.text)
        stock_name = html_detail.xpath('//*[@class="quote_title_l"]/span/@title')
        stock_code = html_detail.xpath('//*[@class="quote_title_l"]/span[2]/text()')
        table = html_detail.xpath('//*[@class="quotecore"]/table/tbody/tr/td')
        item['stock_code'] = stock_code[0]
        item['stock_name'] = stock_name[0]
        profit = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[1]/td[1]/*[@class="price_draw"]/text()')  # 收益
        pe_dynamic = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[1]/td[2]/*[@class="price_draw"]/text()')  # PE(动)
        item['profit'] = value_empty(profit)
        item['pe_dynamic'] = value_empty(pe_dynamic)

        net_asset_per_share = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[2]/td[1]/*[@class="price_draw"]/text()')  # 每股净资产
        market_value_to_net_asset = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[2]/td[2]/*[@class="price_draw"]/text()')  # 市净率
        item['net_asset_per_share'] = value_empty(net_asset_per_share)
        item['market_value_to_net_asset'] = value_empty(market_value_to_net_asset)

        total_revenue = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[3]/td[1]/*[@class="price_draw"]/text()')  # 总营收
        revenue_growth = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[3]/td[2]/*[@class="price_draw"]/text()')  # 总营收同比
        item['total_revenue'] = value_empty(total_revenue)
        item['revenue_growth'] = value_empty(revenue_growth)

        net_profit = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[4]/td[1]/*[@class="price_draw"]/text()')  # 净利润
        profit_growth = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[4]/td[2]/*[@class="price_draw"]/text()')  # 净利润同比
        item['net_profit'] = value_empty(net_profit)
        item['profit_growth'] = value_empty(profit_growth)

        gross_margin = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[5]/td[1]/*[@class="price_draw"]/text()')  # 毛利率
        net_profit_margin = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[5]/td[2]/*[@class="price_draw"]/text()')  # 净利率
        item['gross_margin'] = value_empty(gross_margin)
        item['net_profit_margin'] = value_empty(net_profit_margin)

        return_on_equity = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[6]/td[1]/*[@class="price_draw"]/text()')  # ROE
        liability_ratio = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[6]/td[2]/*[@class="price_draw"]/text()')  # 负债率
        item['return_on_equity'] = value_empty(return_on_equity)
        item['liability_ratio'] = value_empty(liability_ratio)

        total_shares = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[7]/td[1]/*[@class="price_draw"]/text()')  # 总股本
        market_value = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[7]/td[2]/*[@class="price_draw"]/text()')  # 总值
        item['total_shares'] = value_empty(total_shares)
        item['market_value'] = value_empty(market_value)

        circulation_shares = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[8]/td[1]/*[@class="price_draw"]/text()')  # 流通股
        circulation_value = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[8]/td[2]/*[@class="price_draw"]/text()')  # 流值
        item['circulation_shares'] = value_empty(circulation_shares)
        item['circulation_value'] = value_empty(circulation_value)

        # 每股未分配利润
        undistributed_profit_per_share = html_detail.xpath(
            '//*[@class="quotecore"]/table/tbody/tr[9]/td[1]/*[@class="price_draw"]/text()')
        item['undistributed_profit_per_share'] = value_empty(undistributed_profit_per_share)

        yield item

